{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import datetime as dt\n",
    "import pandas as pd\n",
    "\n",
    "from gs_quant.markets.position_set import Position, PositionSet\n",
    "from gs_quant.session import Environment, GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "client = 'CLIENT ID'\n",
    "secret = 'CLIENT SECRET'\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id=client, client_secret=secret, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "If you have historical positions that you'd like to bring into Marquee, you should create an individual position\n",
    "set per each position date. The below examples show the three valid ways to do this (column names/format included)\n",
    "\n",
    "### Step 1: Define your file format\n",
    "#### Option 1: Create historical position sets by weight\n",
    "<table>\n",
    "    <th>identifier</th><th>weight</th><th>date</th>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>0.4</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>0.6</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>0.4</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>0.2</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>IBM UN</td>\n",
    "        <td>0.4</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "columns = ['identifier', 'weight', 'date']\n",
    "equalize = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "#### Option 2: Create historical position sets by quantity\n",
    "\n",
    "<table>\n",
    "    <th>identifier</th><th>quantity</th><th>date</th>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>100</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>100</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>200</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>300</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>IBM UN</td>\n",
    "        <td>200</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "columns = ['identifier', 'quantity', 'date']\n",
    "equalize = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "#### Option 3: Create equally-weighted historical position sets\n",
    "<table>\n",
    "    <th>identifier</th><th>date</th>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>2022-06-03</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>AAPL UW</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>MSFT UW</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>IBM UN</td>\n",
    "        <td>2023-01-04</td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "columns = ['identifier', 'date']\n",
    "equalize = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Step 2: Upload your excel file and convert your positions into position sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "positions_data = pd.read_excel('positions_data.xlsx', engine='openpyxl')\n",
    "all_positions = positions_data[columns]\n",
    "\n",
    "positions_dict, position_sets = {}, []\n",
    "for i, row in all_positions.iterrows():\n",
    "    date = row.get('date')\n",
    "    if date not in positions_dict:\n",
    "        positions_dict[date] = set()\n",
    "    positions_dict[date].add(\n",
    "        Position(identifier=row.get('identifier'), weight=row.get('weight', None), quantity=row.get('quantity', None))\n",
    "    )\n",
    "\n",
    "for date in positions_dict:\n",
    "    position_date = dt.datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S').date()\n",
    "    position_set = PositionSet(positions=positions_dict[date], date=position_date)\n",
    "    if equalize:\n",
    "        position_set.equalize_position_weights()\n",
    "    position_sets.append(position_set)\n",
    "\n",
    "print(position_sets)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}